﻿
Ext.require([
    'Ext.form.*',
    'Ext.data.*',
    'Ext.grid.Panel',
    'Ext.layout.container.Column'
]);


Ext.onReady(function(){
	
    Ext.QuickTips.init();

    var bd = Ext.getBody();

	Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [{name: 'VOZAC_ID', type:'integer'},'IME','PREZIME','SPOL','EMAIL','MOBITEL','SLIKA','STATUS','BILJESKE','TIME_CREATED','TIME_MODIFIED']
	}); 

	var ds = Ext.create('Ext.data.Store', {
		model: 'User',
		autoLoad: true,
		autoSync: true,
		sortOnLoad: true,
		sorters: {property: 'VOZAC_ID', direction: 'ASC'},
		proxy: {
			type: 'ajax',
			url : 'data.php',
			reader: {
				type: 'json'
			}
		}
	});

	

	


    var gridForm = Ext.create('Ext.form.Panel', {
        frame: true,
        title: 'VOZAČI',
        bodyPadding: 5,
        width: 1000,
        layout: 'column',

        fieldDefaults: {
            labelAlign: 'left',
            msgTarget: 'side'
        },

        items: [{
            columnWidth: 0.60,
            xtype: 'gridpanel',
            store: ds,
            height: 400,
			forceFit: true,
            title:'Grid vozača',

            columns: [
				{
					header: 'VOZAC_ID',
					sortable: true,
					dataIndex: 'VOZAC_ID',
					hidden: true
				},{
					header: 'Ime',
					sortable: true,
					dataIndex: 'IME'
                },{
					header: 'Prezime',
					sortable: true,
					dataIndex: 'PREZIME'
                },{
					header: 'Spol',
					sortable: true,
					dataIndex: 'SPOL'
                },{
					header: 'Status',
					sortable: true,
					dataIndex: 'STATUS'
                },{
					header: 'E-mail',
					sortable: true,
					dataIndex: 'EMAIL'
                },{
					header: 'Mobitel',
					sortable: true,
					dataIndex: 'MOBITEL'
                },{
					header: 'Bilješke',
					sortable: true,
					dataIndex: 'BILJESKE',
					hidden: true
                },{
					header: 'Slika',
					sortable: true,
					dataIndex: 'SLIKA',
					hidden: true
                },{
					header: 'TIME_CREATED',
					sortable: true,
					dataIndex: 'TIME_CREATED',
					hidden: true
                },{
					header: 'TIME_MODIFIED',
					sortable: true,
					dataIndex: 'TIME_MODIFIED',
					hidden: true
                }
            ],

            listeners: {
                selectionchange: function(model, records) {
                    if (records[0]) {
                      var record = {
                        data : {
							  Slika: records[0].data.SLIKA,
                              id: records[0].data.VOZAC_ID,
                              Ime: records[0].data.IME,
                              Prezime: records[0].data.PREZIME,
                              Mobitel: records[0].data.MOBITEL,
                              Mail: records[0].data.EMAIL,
                              Biljeske: records[0].data.BILJESKE,
                              radio1: records[0].data.SPOL,
                              radio2: records[0].data.STATUS
                          }
                       };
						  var Mutant = records[0].data.SLIKA;
						  var MutantUdaraOpet = "upload/";
						  if(Mutant != null)
						  {
							Ext.getCmp('slikaid').getEl().dom.src = MutantUdaraOpet.concat(Mutant.toString());
						  }
						  else
						  {
							Ext.getCmp('slikaid').getEl().dom.src ="../logo_Unidu.png";
						  }
                          gridForm.getForm().loadRecord(record);  
                    }
                }
            }
        }, {
            columnWidth: 0.4,
            margin: '0 0 0 10',
            xtype: 'fieldset',
            title:'Forma za unos/izmjenu/brisanje vozača',
			url: 'handlerforme.php',
            defaults: {
                width: 240,
                labelWidth: 90
            },
            items: [{
            xtype: 'box',
			id: 'slikaid',
			autoEl:
			{
				tag:'img',
				src:'../logo_Unidu.png'
			},
			margin: '0 0 15 70',
			height: 100,
			width: 120
            },{
            xtype: 'numberfield', 
            name: 'id',
            fieldLabel: 'Vozac_ID',
            value: 0,
            minValue: 0,
            maxValue: 4294967295,
			hidden: true
            },{
            xtype: 'textfield',
            name: 'Ime',
			id: 'imeid',
            fieldLabel: 'Ime',
			allowBlank: false,
			blankText: 'Ovo polje morate unijeti',
			emptyText: 'Unesite ime vozača'
            },{
            xtype: 'textfield',
            name: 'Prezime',
			id: 'prezimeid',
            fieldLabel: 'Prezime',
			allowBlank: false,
			blankText: 'Ovo polje morate unijeti',
			emptyText: 'Unesite prezime vozača'
            }, {
			xtype: 'textfield',
            name: 'Mobitel',
            fieldLabel: 'Mobitel'
            }, {
			    xtype: 'textfield',
            name: 'Mail',
            fieldLabel: 'E-mail',
			vtype: 'email',
			vtypeText: 'Ovo polje treba imati ispravnu e-mail adresu. Npr. ime.prezime@gmail.com'
        }, {
            xtype: 'textareafield',
            name: 'Biljeske',
            fieldLabel: 'Bilješke',
			height: 50
        }, {
            xtype: 'radiogroup',
            fieldLabel: 'Spol',
			items: [
				{boxLabel:'Muško    ', name: 'radio1', inputValue: 'M', checked: true},
				{boxLabel:'Žensko   ', name: 'radio1', inputValue: 'Z'}
			]	
        }, {
            xtype: 'radiogroup',
            fieldLabel: 'Status',
			items: [
				{boxLabel:'Aktivan  ', name: 'radio2', inputValue: 'A', checked: true},
				{boxLabel:'Neaktivan', name: 'radio2', inputValue: 'N'}
			]	
        },{
            xtype: 'filefield',
            id: 'form-file',
            fieldLabel: 'Slika',
            name: 'photo-path',
			buttonText: 'Izaberite sliku'
        }],
        }],
		        buttons: [{
            text: 'Spremi',
            handler: function(){
               if(gridForm.getForm().isValid()){                   
                   var postanevrijednosti = gridForm.getForm().getValues(true).replace(/&/g,',');
                    Ext.MessageBox.show({
                       title: 'Spremanje',
                       msg: 'Želite li spremiti ovaj podatak?',
                       width:400,
                       buttons: Ext.MessageBox.YESNO,
                       fn: function (btn, text){
					   if(btn == 'yes')
					   {
                          gridForm.getForm().submit({
                            waitMsg:'Pričekajte...',
                            url: 'handlerforme.php',
                        		success: function(form, action) {
                        	       Ext.Msg.alert('Uspjeh!', action.result.msg);
									ds.load();
									Ext.getCmp('slikaid').getEl().dom.src ="../logo_Unidu.png";
                        	    },
                      	    failure: function(form, action) {
                      	        switch (action.failureType) {
                      	            case Ext.form.Action.CLIENT_INVALID:
                      	                Ext.Msg.alert('Neuspjeh', 'Vrijednosti polja forme nisu ispravne!');
                      	                break;
                      	            case Ext.form.Action.CONNECT_FAILURE:
                      	                Ext.Msg.alert('Neuspjeh', 'Greška u komunikaciji AJAX-om!');
                      	                break;
                      	            case Ext.form.Action.SERVER_INVALID:
                      	               Ext.Msg.alert('Neuspjeh', action.result.msg);
                      	       }
                      	    }
                        	});
						}
						else
						{
							gridForm.getForm().reset();
						}
                       }
                   });	
                }
            }
        }, {
			text: 'Briši',
            handler: function(){
				Ext.getCmp('imeid').disable();
				Ext.getCmp('prezimeid').disable();
               if(gridForm.getForm().isValid()){                   
                    Ext.MessageBox.show({
                       title: 'Brisanje',
                       msg: 'Želite li izbrisati vozača?',
                       width:400,
                       buttons: Ext.MessageBox.YESNO,
                       fn: function (btn, text){
					   if(btn == 'yes')
					   {
                          gridForm.getForm().submit({
                            waitMsg:'Pričekajte...',
                            url: 'brisanje.php',
                        		success: function(form, action) {
                        	       Ext.Msg.alert('Uspjeh!', action.result.msg);
									Ext.getCmp('imeid').enable();
									Ext.getCmp('prezimeid').enable();
									ds.load();
									Ext.getCmp('slikaid').getEl().dom.src ="../logo_Unidu.png";
									
                        	    },
                      	    failure: function(form, action) {
                      	        switch (action.failureType) {
                      	            case Ext.form.Action.CLIENT_INVALID:
                      	                Ext.Msg.alert('Neuspjeh', 'Vrijednosti polja forme nisu ispravne!');
										Ext.getCmp('imeid').enable();
										Ext.getCmp('prezimeid').enable();
                      	                break;
                      	            case Ext.form.Action.CONNECT_FAILURE:
                      	                Ext.Msg.alert('Neuspjeh', 'Greška u komunikaciji AJAX-om!');
										Ext.getCmp('imeid').enable();
										Ext.getCmp('prezimeid').enable();
                      	                break;
                      	            case Ext.form.Action.SERVER_INVALID:
                      	               Ext.Msg.alert('Neuspjeh', action.result.msg);
										Ext.getCmp('imeid').enable();
										Ext.getCmp('prezimeid').enable();
                      	       }
                      	    }
                        	});
							
						}
						else
						{
							gridForm.getForm().reset();
							Ext.getCmp('imeid').enable();
							Ext.getCmp('prezimeid').enable();
							Ext.getCmp('slikaid').getEl().dom.src ="../logo_Unidu.png";
						}
                       }

                   });	
                }
            }

		}, {
            text: 'Resetiraj',
            handler: function(){
                gridForm.getForm().reset();
				Ext.getCmp('slikaid').getEl().dom.src ="../logo_Unidu.png";
            }
        }] ,
        renderTo: 'gridForm',
    });
	


});
